<!--工序搜索-->
<template>
    <div class="project-search-wrapper" ref="projectBoxRef">
        <a-table :scroll="{x:0,y:200}" :columns="columns" :data-source="tableData" :customRow="customClick" bordered :pagination="pagination" :loading="isSearch">
        </a-table>
    </div>
</template>
<script>
import { getAction } from "@/api/manage";

const columns = [
    {
        title: "工序编码",
        dataIndex: "code",
        width: "120"
    },
    {
        title: "工序名称",
        dataIndex: "name",
        width: "120"
    },
    {
        title: "加工方式",
        dataIndex: "processMethod",
        width: "120",
        customRender: (text, row, index) => {
            const type = ["自制", "委外"];
            return type[text - 1];
        }
    },
    {
        title: "工资类型",
        dataIndex: "wagesType",
        width: "120",
        customRender: (text, row, index) => {
            const type = ["计件", "计时", "不核算"];
            return type[text - 1];
        }
    },
    {
        title: "合格品价格",
        dataIndex: "qualifiedPrice",
        width: "120"
    },
];
export default {
    props: {

    },
    data () {
        return {
            pagination: {
                current: 1,
                pageSize: 20,
                total: 0,
                size: 'small',
                onChange: (page, pageSize) => {
                    this.pagination.current = page;
                    this.pagination.pageSize = pageSize;
                    this.getData();
                }
            },
            columns: [],
            tableData: [],
            customClick: (record, index) => ({
                on: {
                    click: (event) => {
                        event.preventDefault();
                        console.log('点击的行数', record, index);
                        this.$emit('check', record)
                    },
                },
            }),
            isSearch: false,

            apiUrl: null,       //请求接口
            inputValue: '',     //输入框的值
        }
    },
    created () {
        this.getData();
        this.checkSearch();
    },
    methods: {
        checkSearch () {
            //当前搜索类型
            // supplie      供应商
            // office       部门
            // employees    业务员、请购人、检验
            // project      项目
            // currency     币种
            // purchaseType 采购类型
            // crm          客户名称
            // saleType     销售类型
            // warehouse    仓库
            // despatchType     运输方式
            let val = this.dynamicData.val;
            switch (val) {
                case 'supplie'://供应商
                    // this.apiUrl = '/vendor/page';
                    this.apiUrl = '/crm/customer/pageForVoucher';
                    this.columns = [
                        // { title: "首拼码", dataIndex: "vendorPym", },
                        { title: "供应商编码", dataIndex: "crmNo", ellipsis: true, },
                        { title: "供应商名称", dataIndex: "crmName", ellipsis: true, },
                        // { title: "类型", dataIndex: "partnerClassId", scopedSlots: { customRender: "partnerClassId" }, },
                        // { title: "联系地址", dataIndex: "address", ellipsis: true, },
                    ];
                    this.postData = {
                        current: this.pagination.current,
                        size: this.pagination.pageSize,
                        isFinance: 1,
                        companyType: 'isSupplier'
                    }
                    break;
                case 'employees'://业务员、请购人、检验
                    this.apiUrl = '/sysUser/user/page';
                    this.columns = [
                        { title: "用户姓名", dataIndex: "realName" },
                        { title: "所属机构", dataIndex: "officeName", ellipsis: true, },
                        { title: "手机号", dataIndex: "mobile" },
                        { title: "电子邮箱", dataIndex: "email", ellipsis: true, },
                    ];
                    this.postData = {
                        current: this.pagination.current,
                        size: this.pagination.pageSize,
                        'page.current': this.pagination.current,
                        'page.size': this.pagination.pageSize,
                        searchKey: this.inputValue
                    }
                    break;
                case 'office'://部门
                    this.apiUrl = '/system/office/page';
                    this.columns = [
                        { title: "编码", dataIndex: "officeCode", },
                        { title: "机构名称", dataIndex: "officeName", ellipsis: true, },
                    ];
                    this.postData = {
                        current: this.pagination.current,
                        size: this.pagination.pageSize,
                        officeCode: this.inputValue
                    }
                    break;
                case 'project'://项目
                    this.apiUrl = '/erp/project/pageList';
                    this.columns = [
                        { title: "项目编码", dataIndex: "code" },
                        { title: "项目名称", dataIndex: "name" },
                        { title: "所属部门", dataIndex: "department" },
                        { title: "负责人", dataIndex: "liablePersonNames" },
                    ];
                    this.postData = {
                        current: this.pagination.current,
                        size: this.pagination.pageSize,
                        searchKey: this.inputValue
                    }
                    break;
                case 'currency'://币种
                    this.apiUrl = '/erp/currency/pageList';
                    this.columns = [
                        { title: "编码", dataIndex: "code" },
                        { title: "币名", dataIndex: "name" },
                        { title: "币符号", dataIndex: "currencySign" },
                        {
                            title: "折算方式",
                            dataIndex: "calDirection",
                            customRender: (text, row, index) => {
                                return text == 1 ? "原币*汇率=本位币" : "原币/汇率=本位币";
                            },
                            ellipsis: true,
                        },
                        {
                            title: "汇率设置方式",
                            dataIndex: "exChangeRateType",
                            customRender: (text, row, index) => {
                                return text == 1 ? "日汇率" : "月汇率";
                            }
                        }
                    ];
                    this.postData = {
                        current: this.pagination.current,
                        size: this.pagination.pageSize,
                        searchKey: this.inputValue
                    }
                    break;
                case 'purchaseType'://采购类型
                    this.apiUrl = '/erp/purchaseType/pageList';
                    this.columns = [
                        { title: "编码", dataIndex: "code" },
                        { title: "名称", dataIndex: "name" },
                        { title: "入库类别", dataIndex: "styleName" }
                    ];
                    this.postData = {
                        current: this.pagination.current,
                        size: this.pagination.pageSize,
                        searchKey: this.inputValue
                    }
                    break;
                case 'saleType'://销售类型
                    this.apiUrl = '/erp/saleType/pageList';
                    this.columns = [
                        { title: "销售类型编码", dataIndex: "code" },
                        { title: "销售类型名称", dataIndex: "name" },
                        { title: "出库类别", dataIndex: "styleName" },
                    ];
                    this.postData = {
                        current: this.pagination.current,
                        size: this.pagination.pageSize,
                        searchKey: this.inputValue
                    }
                    break;
                case 'crm'://客户
                    this.apiUrl = '/crm/customer/pageForVoucher';
                    this.columns = [
                        // { title: "首拼码", dataIndex: "crmSimpleName" },
                        { title: "客户名称", dataIndex: "crmName", ellipsis: true, },
                        { title: "所属地区", dataIndex: "areaName", ellipsis: true, },
                        { title: "所属行业", dataIndex: "industry", ellipsis: true, },
                        { title: "联系地址", dataIndex: "address", ellipsis: true, },
                        // { title: "客户类型", dataIndex: "partnerClassId", ellipsis: true, scopedSlots: { customRender: "partnerClassCustomer" }},
                    ];
                    this.postData = {
                        current: this.pagination.current,
                        size: this.pagination.pageSize,
                        isFinance: 1,
                        companyType: 'isCustomer'
                    }
                    break;
                case 'crmCustomer'://报价单客户
                    this.apiUrl = '/crm/customer/pageForVoucher';
                    this.columns = [
                        // { title: "首拼码", dataIndex: "crmSimpleName" },
                        { title: "客户名称", dataIndex: "crmName", ellipsis: true, },
                        { title: "所属地区", dataIndex: "areaName", ellipsis: true, },
                        { title: "所属行业", dataIndex: "industry", ellipsis: true, },
                        { title: "联系地址", dataIndex: "address", ellipsis: true, },
                        // { title: "客户类型", dataIndex: "partnerClassId", ellipsis: true, scopedSlots: { customRender: "partnerClassCustomer" }},
                    ];
                    this.postData = {
                        current: this.pagination.current,
                        size: this.pagination.pageSize,
                    }
                    break;
                case 'warehouse'://仓库
                    let path = this.$route.path;
                    if (path == '/inventory/bills/purchaseIn' || path == '/inventory/bills/productIn' || path == '/inventory/bills/saleOut' || path == '/inventory/bills/materialOut') {
                        //表示是库存管理里的采购入库
                        this.apiUrl = '/in/warehouse/listMyOperationInWarehouses'
                    } else {
                        this.apiUrl = '/in/warehouse/pageList';
                    }

                    this.columns = [
                        { title: "仓库编码", dataIndex: "whCode" },
                        { title: "仓库名称", dataIndex: "whName", ellipsis: true, },
                        { title: "负责人", dataIndex: "whPerson", ellipsis: true, },
                        { title: "电话", dataIndex: "whTelephone", ellipsis: true, },
                    ];
                    this.postData = {
                        current: this.pagination.current,
                        size: this.pagination.pageSize,
                        keyWord: this.inputValue,
                    }
                    break;
                case 'despatchType'://运输
                    this.apiUrl = '/erp/despatchType/pageList';
                    this.columns = [
                        { title: "发运方式编码", dataIndex: "code" },
                        { title: "发运方式名称", dataIndex: "name" }
                    ];
                    this.postData = {
                        current: this.pagination.current,
                        size: this.pagination.pageSize,
                        searchKey: this.inputValue,
                    }
                    break;
                case 'invoiceType'://发票类型
                    this.apiUrl = '/erp/invoiceType/pageList';
                    this.columns = [
                        { title: "发票类型编码", dataIndex: "code" },
                        { title: "发票类型名称", dataIndex: "name" },
                        {
                            title: "方向",
                            dataIndex: "type",
                            customRender: (text) => {
                                return <span>{text == 1 ? "正发票" : "负发票"}</span>;
                            },
                        },
                    ];
                    this.postData = {
                        current: this.pagination.current,
                        size: this.pagination.pageSize,
                        type: this.changeInvoiceType,
                    }
                    break;
                default:
                    break;
            }
        },
        getData (code) {
            //获取右侧表数据
            let params = {
                current: this.pagination.current,
                size: this.pagination.pageSize,
            };
            if (!this.isSearch) {
                this.isSearch = true;
                getAction("/erp/workProcedure/pageList", params).then((response) => {
                    if (response.code == 200) {
                        const { records, total, current, size } = response.data;
                        this.pagination.total = response.data.total;
                        let page = {
                            total: total,
                            pageSize: size,
                            current: current
                        }
                        this.pagination = Object.assign(this.pagination, page);
                        this.tableData = records;
                    } else {
                        // this.$message.warning(response.message);
                    }
                    this.isSearch = false;
                });
            }

        },
        handleEnter () {
            if (this.tableData != '' && this.tableData != null) {
                this.$emit('check', this.tableData[0])
            } else {
                this.$message.warning('请选择一条数据')
                return;
            }
        },
        changePosition (position) {
            console.log(position);
            this.$nextTick(() => {
                this.$refs.projectBoxRef.style.left = (position.left - 200) + 'px';
            })
        }
    }
}
</script>
<style lang="less">
    .project-search-wrapper {
        max-height: 350px;
        position: fixed;
        z-index: 100;
        border: 1px solid #a7abb0;
        width: 500px;
        // .ant-table-wrapper .ant-table-placeholder {
        //     position: static !important;
        //     top: 0;
        // }
        .ant-table-wrapper {
            background: #eee;
        }
        .ant-table-tbody {
            cursor: pointer;
        }
    }
</style>